package com.example.administrator.exkotlin.ui.fragment

import android.app.Fragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.administrator.exkotlin.R
import java.lang.IllegalArgumentException

/**
 * Fragment的例子
 * */
class FragmentActivity : AppCompatActivity(), LeftFragment.LeftFargmentClickListtner {


    lateinit var rightFragment: RightFragment
    lateinit var leftFragment: LeftFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fragment)
        initDatas()
    }

    private fun initDatas() {
        rightFragment = RightFragment()
        leftFragment = fragmentManager.findFragmentById(R.id.fm_left) as LeftFragment
        leftFragment.mlefLeftFargmentClickListtner = this
        replaceFragemnt(rightFragment)
    }

    fun replaceFragemnt(fragment: Fragment) {
        //
        //获取管理,打开事务
        val transation = fragmentManager.beginTransaction()
        //替换
        transation.replace(R.id.fl_right, fragment)
        //添加返回栈
        transation.addToBackStack(null)
        //提交
        transation.commit()
    }

    override fun leftCheck() {
        val currntFragment = fragmentManager.findFragmentById(R.id.fl_right)
        val changeFramgnet = when (currntFragment) {
            is LeftFragment -> {
                rightFragment
            }
            is RightFragment -> {
                LeftFragment()
            }
            else -> {
                rightFragment
            }
        }
        replaceFragemnt(changeFramgnet)
    }
}
